package com.mass.word;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class DbConn {  
    //链接数据库  
    public Connection getCon(){  
        Connection conn = null;  
        try {  
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
            String user = "root";
            String pwd ="root";
            conn=DriverManager.getConnection(url, user, pwd);  
            
        } catch (ClassNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return conn;  
    }  
    //关闭数据库  
    public void closeConn(){  
        Connection con = getCon();  
        try {  
            if(!con.isClosed()){  
                con.close();  
            }  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
   /* public static void main(String[] args){  
        DbConn db = new DbConn();
        db.getCon();  
        db.closeConn();  
    }  */
  
}  
